package com.yasin.hslidablelistview;

import java.util.ArrayList;
import java.util.List;





import com.yasin.libs.HSlidableListView;
import com.yasin.libs.HSlidableListView.OnFlingListener;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

	RadioGroup radioGroup;
	RadioButton maintainRB,warmRB;
    HSlidableListView listview;
	List<MaintainSensor> mData=new ArrayList<MaintainSensor>();
	List<WarmMessage> wData=new ArrayList<WarmMessage>();
	MAdapter mAdapter;
	WAdapter wAdapter;
	int flag =0 ;//0表示左边选中，1表示右边选中
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		maintainRB = (RadioButton) findViewById(R.id.maintain);
		warmRB = (RadioButton) findViewById(R.id.warm);
		listview = (HSlidableListView) findViewById(R.id.msg_list);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Log.e("yasin","click");
			}
			
		});
		/*
		 * 给该listview设置监听，做出相应操作
		 * */
		listview.setOnFlingListener(new OnFlingListener(){

			@Override
			public void onLeftFling() {
				if(flag == 0){
					warmRB.performClick();
					flag = 1;
				}
				
			}

			@Override
			public void onRightFling() {
				Log.e("y-p","right");
				if(flag == 1){
					maintainRB.performClick();
					flag = 0;
				}
				
			}

			
		});
		getDataT(1);
		
		radioGroup = (RadioGroup) findViewById(R.id.type);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId==R.id.maintain){
					maintainRB.setTextColor(Color.parseColor("#000000"));
					warmRB.setTextColor(Color.parseColor("#ffffff"));
					flag = 0;
					getDataT(1);
				}else if(checkedId==R.id.warm){
					maintainRB.setTextColor(Color.parseColor("#ffffff"));
					warmRB.setTextColor(Color.parseColor("#000000"));
					flag = 1;
					getDataT(2);
				}
			}
		});
		
	}
	
	public void getDataT(int flag){
		if(flag == 1){
			mData.clear();
			for(int i=0;i<7;i++){
				MaintainSensor m = new MaintainSensor();
				m.id = i+"";
				m.fname = "养殖"+i+"厂";
				m.sname = i+"设备";
				m.snum = i+"";
				m.desc = "1药剂更换，包含废液袋更换和纯净水更换 2.进样口过滤头更换 3.传感器清理 4.太阳板清理";
				m.isMaintain = (i%2==0)?"0":"1";
				mData.add(m);
			}
			mAdapter = new MAdapter(MainActivity.this,mData);
			listview.setAdapter(mAdapter);
		}else{
			wData.clear();
			for(int i=0;i<7;i++){
				WarmMessage w = new WarmMessage();
				w.id = i+"";
				w.title = "养殖"+i+"厂";
				w.content = i+"设备";
				w.time = i+"";
				wData.add(w);
			}
			wAdapter = new WAdapter(MainActivity.this,wData);
			listview.setAdapter(wAdapter);
		}
	}
	
	private class MaintainSensor{
		String id,fname,snum,sname,desc,isMaintain;
	}
	
	private class WarmMessage{
		String id,title,content,time;
	}
	
	private class MAdapter extends BaseAdapter{

		Context mContext;
		List<MaintainSensor> data;
		
		public MAdapter(Context c,List<MaintainSensor> m){
			this.mContext = c;
			this.data = m;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return data.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int i, View convertView, ViewGroup arg2) {
			ViewHolder holder;
			if(convertView==null){
				convertView = LayoutInflater.from(mContext).inflate(R.layout.maintain_item, null);
				holder = new ViewHolder();
				holder.changName = (TextView) convertView.findViewById(R.id.chang_name);
				holder.sensorName = (TextView) convertView.findViewById(R.id.sensor_name);
				holder.desc = (TextView) convertView.findViewById(R.id.maintain_desc);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			MaintainSensor maint = data.get(i);
			holder.changName.setText(maint.fname);
			holder.sensorName.setText(maint.sname);
			holder.desc.setText(maint.desc);
			return convertView;
		}
		
		private class ViewHolder{
			TextView changName,sensorName,desc;
		}
		
	}
	
	private class WAdapter extends BaseAdapter{

		Context mC;
		List<WarmMessage> data;
		
		public WAdapter(Context c,List<WarmMessage> d){
			this.mC = c;
			this.data = d;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int i, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView==null){
				holder = new ViewHolder();
				convertView = LayoutInflater.from(mC).inflate(R.layout.item_warm_msg, null);
				holder.title = (TextView) convertView.findViewById(R.id.msg_title);
				holder.time = (TextView) convertView.findViewById(R.id.msg_time);
				holder.content = (TextView) convertView.findViewById(R.id.msg_content);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			WarmMessage warm = data.get(i);
			holder.title.setText(warm.title);
			holder.time.setText(warm.time);
			holder.content.setText(warm.content);
			return convertView;
		}
		
		private class ViewHolder{
			TextView title,time,content;
		}
		
	}

}
